package com.fancy.learncenter.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.BaseBean;
import com.fancy.learncenter.bean.CartoonTeacherListSutendSpeakDataBean;
import com.fancy.learncenter.bean.TeacherEveryDayDetailDataBean;
import com.fancy.learncenter.net.CustomNetCallBack;
import com.fancy.learncenter.net.CustomNetSubscriber;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.ui.activity.base.BaseActivity;
import com.fancy.learncenter.ui.adapter.CartoonTeacherEveryDaySentenceAdapter;
import com.fancy.learncenter.ui.adapter.CartoonTeacherEveryDayWordAdapter;
import com.fancy.learncenter.utils.NetUtil;
import com.fancy.learncenter.utils.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.ResponseBody;
import retrofit2.Response;

public class CartoonTeacherEveryDayDetailActivity extends BaseActivity {

    public static String STUDENT_ID_KEY = "STUDENT_ID_KEY";
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;

    View topView;


    CartoonTeacherEveryDaySentenceAdapter sentenceAdapter;
    CartoonTeacherEveryDayWordAdapter wordAdapter;

    RecyclerView wordRecyclerView;
    @Bind(R.id.rootView)
    LinearLayout rootView;
    String classId;
    String studentId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cartoon_teacher_every_day_detail, Status.STATUS);
        ButterKnife.bind(this);
        initToolbar("听说详情");
        classId = getIntent().getStringExtra(CartoonTeacherClassDetailActivity.CLASS_ID_KEY);
        studentId = getIntent().getStringExtra(STUDENT_ID_KEY);

        topView = LayoutInflater.from(this).inflate(R.layout.activity_cartoon_teacher_every_day_detail_top, rootView, false);
        wordRecyclerView = topView.findViewById(R.id.recyclerView);

        initWord();


        initSentence();

        getTeacherList();
    }

    private void initWord() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        wordRecyclerView.setLayoutManager(linearLayoutManager);


        wordAdapter = new CartoonTeacherEveryDayWordAdapter(this, new ArrayList<TeacherEveryDayDetailDataBean.ResultBean.WordSpeakListBean>());

        wordRecyclerView.setAdapter(wordAdapter);
    }

    private void initSentence() {

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        recyclerView.setLayoutManager(linearLayoutManager);

        sentenceAdapter = new CartoonTeacherEveryDaySentenceAdapter(this, new ArrayList<TeacherEveryDayDetailDataBean.ResultBean.GrammerSpeakListBean>());
        recyclerView.setAdapter(sentenceAdapter);

        sentenceAdapter.setHeaderView(topView, recyclerView);
    }


    private void getTeacherList() {
        Map<String, Object> map = new HashMap<>();
        map.put("classId", classId);
        map.put("studentId", studentId);
        map.put("queryType", "1");

        HttpMehtod.getInstance().listStudentSpeakDoneDetail(map, new CustomNetSubscriber(this, true) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {

                BaseBean<TeacherEveryDayDetailDataBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), TeacherEveryDayDetailDataBean.class);
                CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<TeacherEveryDayDetailDataBean>() {
                    @Override
                    public void success(TeacherEveryDayDetailDataBean dataBean) {
                        wordAdapter.addAll(dataBean.getResult().getWordSpeakList());

                        sentenceAdapter.addAll(dataBean.getResult().getGrammerSpeakList());
                    }

                    @Override
                    public void fail(String errorCode) {


                    }
                }).dealData();
            }

        });
    }
}
